--[[
CastYeller2 - Announce important spell events.

Copyright (C) 2009 Adirelle

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--]]

if not CastYeller2 then return end
local CastYeller2 = CastYeller2 
local L = CastYeller2.L

local mod, super = CastYeller2:NewSinkModule('Broadcast')

local MakeChannelList
do
	local list = {}
	function MakeChannelList(...)
		wipe(list)
		for i = 1, select('#', ...), 2 do
			local k,v = select(i, ...)
			list[tostring(k)] = v
		end
		list.none = L['None']
		return list
	end
end

mod.moduleTitle = L['Chat broadcast']
mod.options = {
	name = mod.moduleTitle,
	type = 'group',
	set = function(info, value)
		mod.db.profile[info[#info]] = value
	end,
	get = function(info)
		return mod.db.profile[info[#info]] or (info.type == 'select' and 'none')
	end,
	args = {
		group = {
			name = L['Group'],
			desc = L['Select how announces are sent to group chat when you are in a raid or a party.'],
			type = 'select',
			values = {
				none = L['None'],
				party = L['Party chat only'],
				raid = L['Raid chat only'],
				dynamic = L['Raid or party chat'],
			},
		},
		raidWarning = {
			name = L['Raid warning'],
			desc = L['Check this to have the announces sent as raid warnings when possible.'],
			type = 'toggle',
		},
		channel = {
			name = L['Channel'],
			desc = L['Select a chat channel to which the announces will be sent.'],
			type = 'select',
			values = function() return MakeChannelList(GetChannelList()) end,
		},
		yell = {
			name = L['Yell'],
			desc = L['Check this to have your character yells the announces.'],
			type = 'toggle',
		},
		say = {
			name = L['Say'],
			desc = L['Check this to have your character says the announces.'],
			type = 'toggle',
		},
		emote = {
			name = L['Emote'],
			desc = L['Check this to have your character "emotes" the announces.'],
			type = 'toggle',
		},
	},
}

mod.announceOptions = {
	group = {
		name = L['Group'],
		desc = L['Check this to send this announce to the group.'],
		type = 'toggle',
		arg = { "broadcast", "group" },
	},
	raidWarning = {
		name = L['Raid warning'],
		desc = L['Check this to send this announce as a raid warning.'],
		type = 'toggle',
		arg = { "broadcast", "raidWarning" },
	},
	channel = {
		name = L['Channel'],
		desc = L['Check this to send this announce to the select chat channel.'],
		type = 'toggle',
		arg = { "broadcast", "channel" },
	},
	yell = {
		name = L['Yell'],
		desc = L['Check this to yell this announce.'],
		type = 'toggle',
		arg = { "broadcast", "yell" },
	},
	say = {
		name = L['Say'],
		desc = L['Check this to have your character says this announce.'],
		type = 'toggle',
		arg = { "broadcast", "say" },
	},
	emote = {
		name = L['Emote'],
		desc = L['Check this to have your character "emotes" this announce. You probably have to add custom templates to have the announce looks right.'],
		type = 'toggle',
	},
}
mod.announceDefaultsDB = { broadcast = { ['*'] = true, emote = false } }

function mod:OnRegister()
	self.db = self.core.db:RegisterNamespace("Sink_Broadcast", {
		profile = {
			group = 'dynamic',
			raidWarning = false,
			channel = 'none',
			yell = false,
			say = false,
			emote = false,
		},
	})
end

local sinks = {
	group = function(message, group)
		if (group == 'dynamic' or group == 'raid') and GetNumRaidMembers() > 0 then
			SendChatMessage(message, 'RAID')
		elseif (group == 'dynamic' or group == 'party') and GetNumPartyMembers() > 0 then
			SendChatMessage(message, 'PARTY')
		end
	end,
	raidWarning = function(message) 
		if IsRaidLeader() or IsRaidOfficer() or (GetNumRaidMembers() == 0 and GetNumPartyMembers() > 0) then
			SendChatMessage(message, 'RAID_WARNING') 
		end
	end,
	channel = function(message, channel) SendChatMessage(message, 'CHANNEL', nil, channel) end,
	yell = function(message) SendChatMessage(message, 'YELL')	end,
	say = function(message) SendChatMessage(message, 'SAY') end,
	emote =  function(message) SendChatMessage(message, 'EMOTE') end,
}

function mod:Sink(announce, info)
	local message = info.message
	if self.core.db.profile.squareBrackets then
		message = '<< '..message..' >>'
	end
	local broadcast = announce.broadcast
	for name, func in pairs(sinks) do
		local setting = self.db.profile[name]
		if broadcast[name] and setting and setting ~= 'none' then
			func(message, setting)
		end
	end
end

